package patrol.mobile.com.mobilepatrol.activity.security.emergency;

import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencySaveCheckLeader1ListAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.OrgListBean;
import patrol.mobile.com.mobilepatrol.bean.SaveCheckLeaderBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

/**
 * Created by K on 2018/9/14.
 */
public class EmergencySaveCheckLeader1Activity extends CommonListActivity {
    private View itemTopView;
    private MySpinner spOrgName;
    private MySpinner spProblem;
    private ImageView ivSearch;
    private String orgId = "";
    private String problemType = "";
    private List<String> orgNameList = new ArrayList<>();
    private List<String> orgIdList = new ArrayList<>();

    @Override
    public void initView() {
        super.initView();

        itemTopView = LayoutInflater.from(this).inflate(R.layout.activity_emergency_save_check_leader_top, null);
        spOrgName = itemTopView.findViewById(R.id.spOrgName);
        spProblem = itemTopView.findViewById(R.id.spProblem);
        ivSearch = itemTopView.findViewById(R.id.iv_search);
        ivSearch.setVisibility(View.VISIBLE);
        flContent.addView(itemTopView);

        spOrgName.getmText().setTextSize(TypedValue.COMPLEX_UNIT_DIP, 11);
        spProblem.getmText().setTextSize(TypedValue.COMPLEX_UNIT_DIP, 11);

    }

    @Override
    public void initTitle() {
        super.initTitle();
    }

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new EmergencySaveCheckLeader1ListAdapter(this);
    }

    @Override
    protected String initTitleName() {
        return ConsTitles.EMERGENCY_SAVE_CHECK_LEADER_1;
    }

    @Override
    public void configAdapter() {
        super.configAdapter();
        getOrg();
        final List<String> problems = new ArrayList<>();
        problems.add("安全隐患");
        problems.add("其他安全问题");
        spProblem.setmDatas(problems, new MySpinner.CallBack() {
            @Override
            public void onSelect(int position) {
//                if (problems.get(position).equals("安全隐患")){
//                    problemType =  "0";
//                }else if (problems.get(position).equals("其他安全问题")){
//                    problemType =  "1";
//                }
            }
        });
    }

    @Override
    public void setListener() {
        super.setListener();
        spOrgName.getmText().setText(UserManager.getUser().bmname);

        ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                problemType = spProblem.getmText().getText().toString().equals("安全隐患") ? "0" : "1";
                String orgNme = spOrgName.getmText().getText().toString();
                for (int i = 0; i < orgNameList.size(); i++) {
                    if (orgNme.equals(orgNameList.get(i))) {
                        orgId = orgIdList.get(i);
                        requestData(orgId, problemType);
                        return;
                    }
                }
            }
        });


        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //startActivity(EmergencySaveCheck2Activity.class);
                //startActivity(EmergencySaveCheck1Activity.class);
            }
        });
    }

    private void getOrg() {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("objType", "分局,总局");
        hashMap.put("orgId", UserManager.getOrgNo());
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getOrgList);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(final String code, String response) {
                final OrgListBean orgListBean = new Gson().fromJson(response, OrgListBean.class);
                orgNameList = new ArrayList<>();
                for (OrgListBean.ListBean listBean : orgListBean.getList()) {
                    orgNameList.add(listBean.getOrgName());
                    orgIdList.add(listBean.getId());
                }
                if (orgNameList != null && orgNameList.size() > 0) {
                    spOrgName.getmText().setText(orgNameList.get(0));
                }
                getData();

                spOrgName.setmDatas(orgNameList, new MySpinner.CallBack() {
                    @Override
                    public void onSelect(int postion) {
                        // orgId = orgListBean.getList().get(postion).getId();
                    }
                });
            }
        });
    }

    private void getData() {
        problemType = spProblem.getmText().getText().toString().equals("安全隐患") ? "0" : "1";
        String orgNme = spOrgName.getmText().getText().toString();
        for (int i = 0; i < orgNameList.size(); i++) {
            if (orgNme.equals(orgNameList.get(i))) {
                orgId = orgIdList.get(i);
                requestData(orgId, problemType);
                return;
            }
        }

    }

    private void requestData(String orgId, String problemType) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.EMERGENCY_SAVE_CHECK_LEADER);
        HashMap<String, String> map = new HashMap<>();
        map.put("orgId", orgId);
        map.put("problemType", problemType);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                Log.e("!!!!!!!", response);
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();
                JsonArray listArray = jo.getAsJsonArray("list");
                List<SaveCheckLeaderBean> saveCheckLeaderBeanList = GsonUtil.jsonToList(listArray.toString(), SaveCheckLeaderBean.class);
                mAdapter.updateItems(saveCheckLeaderBeanList);
            }
        });
    }

}
